<?php 
namespace clt\aisle;
/**
 * 缓存基础类
 */
use clt\Curl;
use think\Exception;
class unionty{
	public $pay_code = "unionty";
	public $api='https://sd-app1.tfb8.com'; //接口地址
	public $certType='02';   //证件类型 01：身份证02：军官证03：护照04：户口簿05：回乡证06：其他
	public $paycode;
	public $bizType = "5601"; //业务类型
	public $version = '1.0';//版本号
	public $cost_free = 0.5;//单笔成本费率
	public $cost_rate = 0.45;//成本刷卡费率
	public function config(){
     	$i=0;
     	$modules['code']  = $this->pay_code ;
		$modules['jinjian']   = 1 ;  //是否需要进件 0 不需要 1 需要
		$modules['name']    = '天亿快捷';

		/* 描述对应的语言项 */
		$modules['desc']    = '天亿快捷收款';

		/* 作者 */
		$modules['author']  = 'Perry Lin';
	    /* 版本号 */
	    $modules['version'] = 'V1.0.0';

	    /* 配置信息 */
	    $modules['config']  = array(
	        array('name' => 'branchId',   'type' => 'text',   'value' => '', 'label'=>'机构号'),
	        array('name' => 'branchKey',   'type' => 'text',   'value' => '', 'label'=>'商户密钥'),
	        //array('name' => 'SHAKEY',   'type' => 'text',   'value' => '', 'label'=>'SHAKEY'),
	        array('name' => 'consRate',            'type' => 'text',   'value' => '', 'label'=>'费率(%)'),
	        array('name' => 'consFee',           'type' => 'text',   'value' => '', 'label'=>'手续费(元/笔)'),
	        array('name' => 'callback',           'type' => 'text',   'value' => 'http://'.$_SERVER['SERVER_NAME'].'/mobile/pay_callback.php', 'label'=>'支付成功跳转'),
	    );
	    /*接口通道*/
		$modules['aisle']  = array(
			array('pay_code'=>$pay_code,'code'=>'doOrder','payment'=>'unionpay','name'=>'天亿快捷收款','status'=>0),
		);
		
		return $modules;
	}
	/**
	*  商户进件
	*  @param   array   $user      用户id
	*  @param   array   $config    用户支付配置信息
	*  return   array   code=> 0 失败 1成功 ，data=> 进件返回数据
	**/
	public function reg($user,$config){
		
	}
	//盈起快捷
	public function doOrder($order,$config){
		$data=array();
		$data['version'] = $this->version;
		$data['merchId']=$config['mchtNo'];
		$data['orderId']=$order['order_no'];
		$data['payamt']=$order['total_fee'];
		$data['bizType'] = $this->bizType;
		$data['accNo']=$order['account']['bankAccountNo'];
		$data['lpName']=$order['account']['bankAccountName'];
		$data['phoneNo']=$order['account']['bindPhone'];
		$data['lpCertNo'] = $order['account']['idNum'];
		$data['CVN2']=$order['account']['cvn2'];
		$data['expDate'] = $order['account']['expDate'];
		$data['notifyUrl']  = U('wap/notice/unionty');
		//echo 'http://'.$_SERVER['SERVER_NAME'].'/mobile/unionty_notify.php';
		$data['branchId'] = $config['branchId'];
		$params = $this->getParams($data,$config);
		$result = Curl::_post($this->api."/jk/pay/doOrder",array('params' => $params));
		$result = json_decode($result,true);
		$result = base64_decode($result['params']);
		$result = json_decode($result,true);
		//var_dump($data);exit;
		if(isset($result['resCode']) && intval($result['resCode']) == 0)
		{
			$return['err_code']=0;
			$return['err_msg']='付款成功';
			$r = array(
					'payMent'=>'unionpay',
					'settle_rate'=>$config['settle_rate'],
					'shua_rate'=>$config['shua_rate'],
					'fee'=>$config['fee'],
					'orderNo'=>$data['orderId'],
					'orderDate'=>time(),
					'single_fees' => intval($config['fee'])-$this->cost_free,
			);
			$return['callbock'] = $result['url'];
			$return['data'] = $r;
			return $return;
		}
		$return=array();
		$return['err_code']=1;
		if(!isset($result['resCode'])){
			throw new Mexception("请求失败。", -1);
			
		}
		throw new Mexception($result['resMsg'], $result['resCode']);
		
	}
	/*应答回调*/
	public function respon($data){
		if(intval($data['rspCode']) != 0){
			return $data['rspMsg']; //交易金额不对
		}
		$sign = $data['sign'];
		unset($data['sign']);
		$config = model('Payment')->getConfig('unionty');
		
		if($sign != $this->getSign($data,$config)){
			return '签名不对'; //交易金额不对
		}
		sleep(5);
		$map['order_no'] = $data['orderId'];
		$map['mch_id'] = $data['merchId'];
		$order = model('Payment')->field('id,total_fee,order_no,uid,mch_id,settle_status,order_status')->where($map)->find();
		
		if(empty($order)){  //订单不存在在
			return "notify_success"; //notify_success
		}
		if($order['settle_status'] == 1 || $order['order_status'] == 5){
			return "notify_success"; //订单状态已提现处理
		}
		$result = $this->settle($order,$config);//提现	
		if($result['err_code'] == 0){
			//此处更改订单状态并计算佣金
			$content=$result['err_msg'];
			order_paid($order['id'],5,$order['total_fee'],$content);
			$result['err_msg'] = 'notify_success';
		}else{
			$content=$result['err_msg'];
			order_paid($order['id'],6,$order['total_fee'],$content);
			if($order['order_status'] != 6){
				/*消息提示*/
				$wechatObj = new WechatClass();
				$wechatObj->sendMassge($order['uid'],array(
					'content'=>"收款失败通知:\n收款金额为".$order['total_fee']."元,银行处理失败,请联系客服处理...",
				));
			}
		}
		
		
		return $result['err_msg'];//$result = $this->settle($order,$temp);
	}
	/*费率查询接口*/
	public function queryfee($config){
		$return['code']='1011';
		$return['msg']='不支持费率查询';
		return $return;
	}
	/*费率修改接口*/
	public function updatefee($config){
		$return['code']='1011';
		$return['msg']='不支持费率修改';
		return $return;	
	}
	/*绑定信用卡*/
	public function bingCardSMS($config,$account){
	
	}
	/*绑定银行卡*/
	public function bindCardConfirm($config,$account){
		
	}
	/*钱包查询*/
	public function quickpayBalance($config){
		
	}
	/*提现记录*/
	public function settleLog(){

	}
	/**
	* 修改商户信息
	* @param array $field  修改的数据
	* @param array $mch    商户的数据
	* return   array   code=> 0 失败 1成功 ，data=> 进件返回数据
	**/
	public function changeinfo($field,$mch){
		$return=array(
			'code' => 1,
			'data' =>''
		);
		return $return;
	}

	/**
	* 修改结算卡
	* @param array $field  修改的数据
	* @param array $mch    商户的数据
	* return   array   code=> 0 失败 1成功 ，data=> 进件返回数据
	**/
	public function changebank($field,$mch){


		

	}
	/*获取到签名*/
	public function getSign($data,$config=array()){
		ksort($data);
		$params = [];
		foreach($data as $field=>$value){
			$params[] = $field."=".$value;
		}
		$Sign = Md5(implode("&", $params).$config['branchKey']);
		return strtoupper($Sign);
	}
	/*获取参数*/
	private function getParams($data,$config=array()){
		$sign = $this->getSign($data,$config);
		$data['sign'] = $sign;
		return $params = base64_encode(json_encode($data));
	}
}